package com.svpino.netflix;

import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Element;

import com.svpino.netflix.model.Item;
import com.svpino.netflix.parsers.ListParser;


public class LateBindingList<T extends Item> implements Iterable<T>{
	
	private List<T> list;
	private ListParser<T> parser;
	private Element[] elements;
	private String[] urls;
	
	public LateBindingList(ListParser<T> parser, Element... elements) {
		this.parser = parser;
		this.elements = elements;
		this.list = parser.list(this.elements);
	}
	
	public LateBindingList(ListParser<T> parser, String... urls) {
		this.parser = parser;
		this.urls = urls;
	}
	
	@Override
	public Iterator<T> iterator() {
		if (this.list == null) {
			synchronized (this) {
				if (this.list == null) {
					this.list = this.parser.list(this.urls);
				}
			}
			
		}
		
		return this.list.iterator();
	}

}
